unit Interface_DebugConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, Core_SettingsEngine, StdCtrls;

type
  TDebug_Config = class(TForm)
    TreeView: TTreeView;
    GroupBox: TGroupBox;
    Label1: TLabel;
    lblDataLen: TLabel;
    txtData: TMemo;
    rbxHex: TRadioButton;
    rbxText: TRadioButton;
    rbxInt: TRadioButton;
    btnSave: TButton;
    btnClearData: TButton;
    procedure FormShow(Sender: TObject);
    procedure TreeViewClick(Sender: TObject);
    procedure rbxHexClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnClearDataClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Debug_Config: TDebug_Config;
  CurentKnot : PDataKnot;

implementation

{$R *.dfm}

procedure TDebug_Config.FormShow(Sender: TObject);
  procedure AddSettingNode(Node : TTreeNode; SetNode : PDataKnot);
  var
    tNode : PDataKnot;
    rNode : TTreeNode;
  begin
    tNode := SetNode;
    while tNode <> nil do
    begin
      rNode := TreeView.Items.AddChild(Node, tNode.Name);
      rNode.Data := tNode;
      if tNode.FirstChild <> nil then
      begin
        AddSettingNode(rNode, tNode.FirstChild);
      end;
      tNode := tNode^.Next;
    end;
  end;
var
  Root : TTreeNode;
begin
  TreeView.Items.Clear;
  Root := TreeView.Items.AddChildFirst(nil, 'Config');
  AddSettingNode(Root, ConfigRoot);
  Root.Expand(False);
end;

procedure TDebug_Config.TreeViewClick(Sender: TObject);
begin
  if TreeView.Selected.Data <> nil then
  begin
    GroupBox.Caption := TDataKnot(TreeView.Selected.Data^).Name;
    lblDataLen.Caption := IntToStr(TDataKnot(TreeView.Selected.Data^).DataLength) + ' byte';
    CurentKnot := TreeView.Selected.Data;
    rbxHexClick(Sender);
  end;
end;

procedure TDebug_Config.rbxHexClick(Sender: TObject);
  function DataToHex(Data : Pointer; Len : Integer) : String;
  type
    TBArray = array[0..0] of Byte;
  var
    i:Integer;
  begin
    Result := '';
    for i := 0 to Len-1 do
    begin
      Result := Result + IntToHex(TBArray(Data^)[i], 2) + ' ';
    end;
  end;
begin
  if (CurentKnot <> nil) and (CurentKnot^.Data <> nil) then
  begin
    if rbxHex.Checked then
      txtData.Text := DataToHex(TDataKnot(CurentKnot^).Data, TDataKnot(TreeView.Selected.Data^).DataLength);

    if rbxText.Checked then
      txtData.Text := PChar(TDataKnot(CurentKnot^).Data);

    if rbxInt.Checked then
      txtData.Text := IntToStr(Integer(TDataKnot(CurentKnot^).Data^));
  end else
    txtData.Text := '';
end;

procedure TDebug_Config.btnSaveClick(Sender: TObject);
var
  s:String;
  i:Integer;
begin
  if (CurentKnot <> nil) then
  begin
    if rbxHex.Checked then
      ShowMessage('TODO');

    if rbxText.Checked then
    begin
      s := txtData.Text;

      CurentKnot^.DataLength := Length(s)+1;
      GetMem(CurentKnot^.Data, CurentKnot^.DataLength);
      ZeroMemory(CurentKnot^.Data, CurentKnot^.DataLength);

      CopyMemory(CurentKnot^.Data, @s[1], CurentKnot^.DataLength-1);
    end;

    if rbxInt.Checked then
    begin
      i := StrToInt(txtData.Text);
      CurentKnot^.DataLength := SizeOf(Integer);
      GetMem(CurentKnot^.Data, SizeOf(Integer));
      CopyMemory(CurentKnot^.Data, @i, SizeOf(Integer));
    end;
  end;

  rbxHexClick(Sender);
end;

procedure TDebug_Config.btnClearDataClick(Sender: TObject);
begin
  if CurentKnot <> nil then
  begin
    if CurentKnot^.DataLength <> 0 then
    begin
      CurentKnot^.DataLength := 0;
      CurentKnot^.Data := nil;
    end;
  end;

  rbxHexClick(Sender);
end;

end.
